import { handleRankingScrollTableConditions } from '../conditionFormat/useConditionFormat.js';

export function rankingScrollTableFormat(dataSets) {
  const { dimensions, conditionOptions, fieldNameChainMap, type, collectdataSetType, source } = dataSets;
  let result = [];
  let realData = []; // 原始数据
  let timeStampData = {}; // 时间类型数据
  let dimensionName = []; // 时间类型数据名称
  const columns = Object.keys(source[0] || {});
  if (type !== 'STATIC') {
    const nameKey = dimensions?.xAxis;
    const valueKey = dimensions?.yAxis;
    const typeNameList = Object.keys(collectdataSetType) && Object.keys(collectdataSetType).filter((type) => type === 'TIMESTAMP');
    dimensionName = typeNameList.map((name) => collectdataSetType[name]);
      source.forEach((dataItem) => {
      result.push({
        name: dataItem[nameKey] ? dataItem[nameKey] : '',
        value: dataItem[valueKey] ? dataItem[valueKey] : '',
      });
    });
    realData = source;
    // 查找时间类型数据
    dimensionName.forEach((dimensionName) => {
      const result = realData.map((item) => {
        return {
          value: item[dimensionName],
        };
      });
      timeStampData[dimensionName] = result;
    });
    // 处理条件格式
    const componentAttrs = {
      conditionOptions,
      config: {
        header: {
          name: columns[0],
          value: columns[1],
        },
        realData, // 原始数据
        sourceFields: {
          name: fieldNameChainMap['xAxis'] && Object.values(fieldNameChainMap['xAxis'])[0],
          value: fieldNameChainMap['yAxis'] && Object.values(fieldNameChainMap['yAxis'])[0],
        },
        data: result,
      },
      timeStampData,
    };
    handleRankingScrollTableConditions(componentAttrs);
  } else {
    result = source;
  }


  return {
    formatData: {
      data: result,
      header: {
        name: columns[0],
        value: columns[1],
      },
      realData, // 原始数据
    },
  };
}
